package com.ai.daofa; /*
 * @author: changchun.liang
 * @date: 2024/4/6
 * @Email: liangcc0309@dingtalk.com
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

public class Code {
    public static void main(String[] args) throws IOException {
        //BrainFace face = new BrainFace();
       // face.practiceInWord("我是谁");

        //System.out.println(decrypt());
        //JobParam param = new JobParam();
        /*
        String jsonStr = "{'startDate':'2024-04-16 00:00:00','endDate':'2024-04-17 23:59:59'}";
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("param", JSONObject.parse(jsonStr));
        map.put("logId",30);
        //map.put("decrypt", RsaSymmetUtils.decrypt());
        String logId = String.valueOf(map.get("logId"));
        test(map);
*/

        String startDateStr = "2024-04-16 00:00:00";
        String endDateStr = "2024-04-17 23:59:59";
        Date startDate = DateUtil.parseDate(startDateStr);
        Date endDate = DateUtil.parseDate(endDateStr);
        List<String> calDates =  findDates(startDate, endDate);
        int days = calDates.size();
        String jsonStr = "";
        for (int i = 0;i<days;i++) {
            DateTime dateTime = DateUtil.offsetDay(startDate,i);
            String sDate = DateUtil.formatDate(DateUtil.beginOfDay(dateTime));
            System.out.println(sDate );
            jsonStr = "{'startDate':'"+sDate+" 00:00:00','endDate':'"+sDate+" 23:59:59'}";
            System.out.println(jsonStr );
        }


    }

    public static void test(Map<String,Object> map){

        Date initStartTime = getJobStartParam(map);

        //统计数据的截止时间
        Date         initEndTime = getJobEndParam(map);
        // 计算 开始时间和截止时间内每天的时间集合
        List<String> calDates    = findDates(initStartTime, DateUtil.offsetDay(initEndTime,-1));
        if(CollectionUtil.isEmpty(calDates)){
            System.out.println("return :时间集合为空" );
            return;
        }
        /*
        if(calDates.size()>1){
            System.out.println("渠道注册统计任务注册统计任务一次只能统计一天的数据");
            return;
        }*/
        System.out.println("initStartTime:"+initStartTime.toString()+",initEndTime:"+ initEndTime.toString());
    }



    public static Date getJobStartParam(Map<String,Object> map){
        String param = String.valueOf(map.get("param"));
        if(StrUtil.isNotEmpty(param)){
            JobParam jobParam = JSONObject.parseObject(param,JobParam.class);
            if(jobParam!=null&&jobParam.getStartDate() != null){
                return DateUtil.beginOfDay(DateUtil.parse(jobParam.getStartDate()));
            }
        }
        return  getOneDayStart(new Date(),-1);
    }

    public static Date getJobEndParam(Map<String,Object> map){
        String param = String.valueOf(map.get("param"));
        if(StrUtil.isNotEmpty(param)){
            JobParam jobParam = JSONObject.parseObject(param,JobParam.class);
            if(jobParam!=null&&jobParam.getEndDate() != null){
                return DateUtil.endOfDay(DateUtil.parse(jobParam.getEndDate()));
            }
        }
        return DateUtil.endOfDay(DateUtil.yesterday());
    }

    public static Date getOneDayStart(Date date,int offset){
        DateTime offSetDate = DateUtil.offsetDay(date, offset);   //偏移之后的时间
        return DateUtil.beginOfDay(offSetDate);  //偏移后时间的0点日期
    }

    public static List<String> findDates(Date dStart, Date dEnd) {
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(dStart);

        List<Date> dateList = new ArrayList();
        //别忘了，把起始日期加上
        dateList.add(dStart);
        // 此日期是否在指定日期之后
        while (dEnd.after(cStart.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cStart.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(cStart.getTime());
        }
        List<String> dates = new ArrayList<>();
        if (!isEmpty(dateList)) {
            for (Date d : dateList) {
                dates.add(formatDate(d));
            }
        }
        return dates;
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }


    /**
     * date类型转yyyy-MM-dd
     */
    public static String formatDate(Date date) {
        return DateUtil.formatDate(date);
    }




    /**
     * 加密内容
     */
    public static String decrypt(){
        //公钥加密，私钥解密
        RSA rsa = new RSA(privateKey,publicKey);
        byte[] encrypt = rsa.encrypt(StrUtil.bytes(RSASYS_CODE, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        return HexUtil.encodeHexStr(encrypt);
    }



    private static String RSASYS_CODE = "zthd2020";

    static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCoPOgYC3UkG7fYfJwyeRos4XddwzhJe8oBH1UuFB/IL2pFxx+p9MC3swP1Ruf4McDiKL+Pn6HpK27H07h46VxOQUdD/2+fdd+4nktFd4pi8CC9u+UPjXVNVKQz0HLJ0EtGAOq1hY0QJHuh48Q3DmqunTqMEqp8LPrVlKKtM/+k+QIDAQAB";

    static String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKg86BgLdSQbt9h8nDJ5Gizhd13DOEl7ygEfVS4UH8gvakXHH6n0wLezA/VG5/gxwOIov4+foekrbsfTuHjpXE5BR0P/b59137ieS0V3imLwIL275Q+NdU1UpDPQcsnQS0YA6rWFjRAke6HjxDcOaq6dOowSqnws+tWUoq0z/6T5AgMBAAECgYANIWvP7J0A63Tm3ZrnSZJkbFh/pevvuEyk18gzJ3jvi9vUhF0RS7v62mr1qsoSEfgG7r08vW3KoAwuZnTgYv5EMG49tWR/25r0tgaSxaEQwRDetSaZXH+eibFGnA5QXJX+eQyxbRt+94XrgaYhgBOVQ+rwvt5TwjcVGw7GmVYQAQJBAPZwzVUCQ8qo+OqRwexc0syhM3htqSn52juVjTsI2m4OT+puhoH9XYOXKhgASUeDfZAn/eVDPLPhVLqLIu4SugECQQCuw4nAsELKDaSTlDzQgPmvZ1Po0ALJVSkPW2kbZgKoVrvHWXnY0s9BZ6UC/Mbg7KE8Tu5hgCQRtOOXYfrgpLr5AkEAlc+9AxB9ZZWSvAYffTBwdTUJtQv06i+ZGt+vG1QmdQD+U+Ucn6kGUBh03mJOKcslaZzmG5jkLfWtLDmo4lFAAQJBAIYIk+LDjEXY1ssU2ttyv4xmK/WXazusY7RSgDcyMoGXG6dVmVzQuYHJEsZKxeEFutc9FhiTJZaq7A5TaAzYakkCQGSb0n55CAqWfhvQuOeZ5bz4bgJdWze1LuiwP/wTQgZXvvDpXyMeWZWS1B7vfGdvWcIvRDsEwkn2plKFsuVqBko=";

}
